package com.ego.controller;

import com.ego.dao.RegisterDao;
import com.ego.dao.impl.RegisterDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 袁梦达 2019012364
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    private RegisterDao registerDao = new RegisterDaoImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        InputStream is = request.getInputStream();
        //读取客户端发送的字符串
        StringBuffer stringBuffer = new StringBuffer();
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = is.read(bytes)) != -1){
            stringBuffer.append(new String(bytes, 0, len));
        }

        String[] info = stringBuffer.toString().split(",");
        String userNumber = info[0];
        String password = info[1];
        String name = info[2];

        Integer result = registerDao.register(userNumber, password, name);
        response.getWriter().print(result);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
